Pythonμ μμ± λμμΈ ν¨ν΄μΈ μ±κΈν€, ν©ν 리, μΆμ ν©ν 리, λΉλ, νλ‘ν νμ μ νꡬν©λλ€. κ° ν¨ν΄μ ꡬν, μ₯μ λ° μ€μ μ μ© μ¬λ‘λ₯Ό μμλ΄ λλ€.
Python λμμΈ ν¨ν΄: μμ± ν¨ν΄ μ¬μΈ΅ νꡬ
λμμΈ ν¨ν΄μ μννΈμ¨μ΄ λμμΈμμ νν λ°μνλ λ¬Έμ μ λν μ¬μ¬μ© κ°λ₯ν μ루μ μ λλ€. μ΄λ€μ μ΄λ¬ν λ¬Έμ λ₯Ό ν΄κ²°νκΈ° μν μ²μ¬μ§μ μ 곡νμ¬ μ½λ μ¬μ¬μ©μ±, μ μ§λ³΄μμ± λ° μ μ°μ±μ μ΄μ§ν©λλ€. νΉν μμ± λμμΈ ν¨ν΄μ μν©μ μ ν©ν λ°©μμΌλ‘ κ°μ²΄λ₯Ό μμ±νλ €κ³ λ Έλ ₯νλ©°, κ°μ²΄ μμ± λ©μ»€λμ¦μ λ€λ£Ήλλ€. μ΄ κΈμ Pythonμμ μμ± λμμΈ ν¨ν΄μ λν ν¬κ΄μ μΈ νꡬλ₯Ό μ 곡νλ©°, μμΈν μ€λͺ , μ½λ μμ λ° μ μΈκ³ λ μλ€μκ² κ΄λ ¨ μλ μ€μ μ μ© μ¬λ‘λ₯Ό ν¬ν¨ν©λλ€.
μμ± λμμΈ ν¨ν΄μ΄λ 무μμΈκ°μ?
μμ± λμμΈ ν¨ν΄μ μΈμ€ν΄μ€ν νλ‘μΈμ€λ₯Ό μΆμνν©λλ€. μ΄λ€μ ν΄λΌμ΄μΈνΈ μ½λλ₯Ό μΈμ€ν΄μ€νλλ νΉμ ν΄λμ€λ‘λΆν° λΆλ¦¬νμ¬ κ°μ²΄ μμ±μ λν λ ν° μ μ°μ±κ³Ό μ μ΄λ₯Ό κ°λ₯νκ² ν©λλ€. μ΄λ¬ν ν¨ν΄μ μ¬μ©ν¨μΌλ‘μ¨, μμ±λ κ°μ²΄μ μ νν ν΄λμ€λ₯Ό μ§μ νμ§ μκ³ λ κ°μ²΄λ₯Ό μμ±ν μ μμ΅λλ€. μ΄λ¬ν κ΄μ¬μ¬μ λΆλ¦¬λ μ½λλ₯Ό λ κ°λ ₯νκ³ μ μ§λ³΄μνκΈ° μ½κ² λ§λλλ€.
μμ± ν¨ν΄μ μ£Όμ λͺ©νλ κ°μ²΄ μΈμ€ν΄μ€ν νλ‘μΈμ€λ₯Ό μΆμννμ¬ ν΄λΌμ΄μΈνΈλ‘λΆν° κ°μ²΄ μμ±μ 볡μ‘μ±μ μ¨κΈ°λ κ²μ λλ€. μ΄λ₯Ό ν΅ν΄ κ°λ°μλ κ°μ²΄ μμ±μ μΈλΆμ μΈ μ¬νμ μ½λ§€μ΄μ§ μκ³ μ ν리μΌμ΄μ μ κ³ μμ€ λ Όλ¦¬μ μ§μ€ν μ μμ΅λλ€.
μμ± λμμΈ ν¨ν΄μ μ’ λ₯
μ΄ κΈμμλ λ€μ μμ± λμμΈ ν¨ν΄μ λ€λ£° κ²μ λλ€:
- μ±κΈν€ (Singleton): ν΄λμ€κ° μ€μ§ νλμ μΈμ€ν΄μ€λ§ κ°λλ‘ λ³΄μ₯νκ³ , μ΄μ λν μ μμ μΈ μ κ·Ό μ§μ μ μ 곡ν©λλ€.
- ν©ν 리 λ©μλ (Factory Method): κ°μ²΄λ₯Ό μμ±νκΈ° μν μΈν°νμ΄μ€λ₯Ό μ μνμ§λ§, μ΄λ€ ν΄λμ€λ₯Ό μΈμ€ν΄μ€νν μ§λ μλΈν΄λμ€κ° κ²°μ νκ² ν©λλ€.
- μΆμ ν©ν 리 (Abstract Factory): κ΄λ ¨λκ±°λ μμ‘΄μ μΈ κ°μ²΄λ€μ ν¨λ°λ¦¬λ₯Ό ꡬ체μ μΈ ν΄λμ€λ₯Ό μ§μ νμ§ μκ³ μμ±νκΈ° μν μΈν°νμ΄μ€λ₯Ό μ 곡ν©λλ€.
- λΉλ (Builder): 볡μ‘ν κ°μ²΄μ ꡬμ±μ ννκ³Ό λΆλ¦¬νμ¬, λμΌν κ΅¬μ± νλ‘μΈμ€κ° λ€μν ννμ μμ±ν μ μλλ‘ ν©λλ€.
- νλ‘ν νμ (Prototype): μν μΈμ€ν΄μ€λ₯Ό μ¬μ©νμ¬ μμ±ν κ°μ²΄μ μ’ λ₯λ₯Ό μ§μ νκ³ , μ΄ μνμ 볡μ¬νμ¬ μ κ°μ²΄λ₯Ό μμ±ν©λλ€.
1. μ±κΈν€ ν¨ν΄
μ±κΈν€ ν¨ν΄μ ν΄λμ€κ° μ€μ§ νλμ μΈμ€ν΄μ€λ§ κ°λλ‘ λ³΄μ₯νκ³ , μ΄μ λν μ μμ μΈ μ κ·Ό μ§μ μ μ 곡ν©λλ€. μ΄ ν¨ν΄μ μμ€ν μ λ°μ λμμ μ‘°μ¨νκΈ° μν΄ μ νν νλμ κ°μ²΄κ° νμν λ μ μ©ν©λλ€. 리μμ€ κ΄λ¦¬, λ‘κΉ λλ κ΅¬μ± μ€μ μ μμ£Ό μ¬μ©λ©λλ€.
ꡬν
λ€μμ μ±κΈν€ ν¨ν΄μ Python ꡬνμ λλ€:
class Singleton:
_instance = None
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs)
return cls._instance
# Example usage
s1 = Singleton()
s2 = Singleton()
print(s1 is s2) # Output: True
μ€λͺ :
_instance: μ΄ ν΄λμ€ λ³μλ ν΄λμ€μ μ μΌν μΈμ€ν΄μ€λ₯Ό μ μ₯ν©λλ€.__new__: μ΄ λ©μλλ κ°μ²΄κ° μμ±λ λ__init__λ³΄λ€ λ¨Όμ νΈμΆλ©λλ€. μΈμ€ν΄μ€κ° μ΄λ―Έ μ‘΄μ¬νλμ§ νμΈν©λλ€. μ‘΄μ¬νμ§ μμΌλ©΄super().__new__(cls)λ₯Ό μ¬μ©νμ¬ μ μΈμ€ν΄μ€λ₯Ό μμ±νκ³_instanceμ μ μ₯ν©λλ€. μΈμ€ν΄μ€κ° μ΄λ―Έ μ‘΄μ¬νλ©΄ κΈ°μ‘΄ μΈμ€ν΄μ€λ₯Ό λ°νν©λλ€.
μ¬μ© μ¬λ‘
- λ°μ΄ν°λ² μ΄μ€ μ°κ²°: ν λ²μ νλμ λ°μ΄ν°λ² μ΄μ€ μ°κ²°λ§ μ΄λ € μλλ‘ λ³΄μ₯ν©λλ€.
- μ€μ κ΄λ¦¬μ: μ ν리μΌμ΄μ μ€μ μ λν λ¨μΌ μ κ·Ό μ§μ μ μ 곡ν©λλ€.
- λ‘κ±°: μ ν리μΌμ΄μ μ λͺ¨λ λ‘κΉ μμ μ μ²λ¦¬νκΈ° μν λ¨μΌ λ‘κΉ μΈμ€ν΄μ€λ₯Ό μμ±ν©λλ€.
μμ
μ±κΈν€ ν¨ν΄μ μ¬μ©νμ¬ κ΅¬νλ μ€μ κ΄λ¦¬μμ κ°λ¨ν μμλ₯Ό μ΄ν΄λ³΄κ² μ΅λλ€:
class ConfigurationManager(Singleton):
def __init__(self):
if not hasattr(self, 'config'): # Ensure __init__ is only called once
self.config = {}
def set_config(self, key, value):
self.config[key] = value
def get_config(self, key):
return self.config.get(key)
# Example usage
config_manager1 = ConfigurationManager()
config_manager1.set_config('database_url', 'localhost:5432')
config_manager2 = ConfigurationManager()
print(config_manager2.get_config('database_url')) # Output: localhost:5432
2. ν©ν 리 λ©μλ ν¨ν΄
ν©ν 리 λ©μλ ν¨ν΄μ κ°μ²΄λ₯Ό μμ±νκΈ° μν μΈν°νμ΄μ€λ₯Ό μ μνμ§λ§, μ΄λ€ ν΄λμ€λ₯Ό μΈμ€ν΄μ€νν μ§λ μλΈν΄λμ€κ° κ²°μ νκ² ν©λλ€. ν©ν 리 λ©μλλ ν΄λμ€κ° μΈμ€ν΄μ€νλ₯Ό μλΈν΄λμ€λ‘ μ°κΈ°νλλ‘ ν©λλ€. μ΄ ν¨ν΄μ λμ¨ν κ²°ν©μ μ΄μ§νκ³ κΈ°μ‘΄ μ½λλ₯Ό μμ νμ§ μκ³ λ μλ‘μ΄ μ ν μ νμ μΆκ°ν μ μλλ‘ ν©λλ€.
ꡬν
λ€μμ ν©ν 리 λ©μλ ν¨ν΄μ Python ꡬνμ λλ€:
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def speak(self):
pass
class Dog(Animal):
def speak(self):
return "Woof!"
class Cat(Animal):
def speak(self):
return "Meow!"
class AnimalFactory(ABC):
@abstractmethod
def create_animal(self):
pass
class DogFactory(AnimalFactory):
def create_animal(self):
return Dog()
class CatFactory(AnimalFactory):
def create_animal(self):
return Cat()
# Client code
def get_animal(factory: AnimalFactory):
animal = factory.create_animal()
return animal.speak()
dog_sound = get_animal(DogFactory())
cat_sound = get_animal(CatFactory())
print(f"Dog says: {dog_sound}") # Output: Dog says: Woof!
print(f"Cat says: {cat_sound}") # Output: Cat says: Meow!
μ€λͺ :
Animal: λͺ¨λ λλ¬Ό μ νμ λν μΈν°νμ΄μ€λ₯Ό μ μνλ μΆμ κΈ°λ³Έ ν΄λμ€μ λλ€.Dogλ°Cat:AnimalμΈν°νμ΄μ€λ₯Ό ꡬννλ ꡬ체μ μΈ ν΄λμ€μ λλ€.AnimalFactory: λλ¬Όμ μμ±νκΈ° μν μΈν°νμ΄μ€λ₯Ό μ μνλ μΆμ κΈ°λ³Έ ν΄λμ€μ λλ€.DogFactoryλ°CatFactory: κ°κ°Dogλ°CatμΈμ€ν΄μ€λ₯Ό μμ±νλ μν μ νλAnimalFactoryμΈν°νμ΄μ€λ₯Ό ꡬννλ ꡬ체μ μΈ ν΄λμ€μ λλ€.get_animal: ν©ν 리λ₯Ό μ¬μ©νμ¬ λλ¬Όμ μμ±νκ³ μ¬μ©νλ ν΄λΌμ΄μΈνΈ ν¨μμ λλ€.
μ¬μ© μ¬λ‘
- UI νλ μμν¬: λ€λ₯Έ μ΄μ 체μ μ λν΄ λ€λ₯Έ ν©ν 리λ₯Ό μ¬μ©νμ¬ νλ«νΌλ³ UI μμ(μ: λ²νΌ, ν μ€νΈ νλ)λ₯Ό μμ±ν©λλ€.
- κ²μ κ°λ°: κ²μ λ 벨 λλ μ¬μ©μ μ νμ λ°λΌ λ€λ₯Έ μ νμ κ²μ μΊλ¦ν° λλ κ°μ²΄λ₯Ό μμ±ν©λλ€.
- λ¬Έμ μ²λ¦¬: μνλ μΆλ ₯ νμμ λ°λΌ λ€λ₯Έ ν©ν 리λ₯Ό μ¬μ©νμ¬ λ€λ₯Έ μ νμ λ¬Έμ(μ: PDF, Word, HTML)λ₯Ό μμ±ν©λλ€.
μμ
μ¬μ©μ μ νμ λ°λΌ λ€μν μ νμ κ²°μ λ°©λ²μ μμ±νλ €λ μλ리μ€λ₯Ό κ³ λ €ν΄ λ³΄μΈμ. λ€μμ ν©ν 리 λ©μλ ν¨ν΄μ μ¬μ©νμ¬ μ΄λ₯Ό ꡬννλ λ°©λ²μ λλ€:
from abc import ABC, abstractmethod
class Payment(ABC):
@abstractmethod
def process_payment(self, amount):
pass
class CreditCardPayment(Payment):
def process_payment(self, amount):
return f"Processing credit card payment of ${amount}"
class PayPalPayment(Payment):
def process_payment(self, amount):
return f"Processing PayPal payment of ${amount}"
class PaymentFactory(ABC):
@abstractmethod
def create_payment_method(self):
pass
class CreditCardPaymentFactory(PaymentFactory):
def create_payment_method(self):
return CreditCardPayment()
class PayPalPaymentFactory(PaymentFactory):
def create_payment_method(self):
return PayPalPayment()
# Client code
def process_payment(factory: PaymentFactory, amount):
payment_method = factory.create_payment_method()
return payment_method.process_payment(amount)
credit_card_payment = process_payment(CreditCardPaymentFactory(), 100)
paypal_payment = process_payment(PayPalPaymentFactory(), 50)
print(credit_card_payment) # Output: Processing credit card payment of $100
print(paypal_payment) # Output: Processing PayPal payment of $50
3. μΆμ ν©ν 리 ν¨ν΄
μΆμ ν©ν 리 ν¨ν΄μ ꡬ체μ μΈ ν΄λμ€λ₯Ό μ§μ νμ§ μκ³ κ΄λ ¨λκ±°λ μμ‘΄μ μΈ κ°μ²΄λ€μ ν¨λ°λ¦¬λ₯Ό μμ±νκΈ° μν μΈν°νμ΄μ€λ₯Ό μ 곡ν©λλ€. μ΄ ν¨ν΄μ ν¨κ» μλνλλ‘ μ€κ³λ κ°μ²΄λ₯Ό μμ±νμ¬ μΌκ΄μ±κ³Ό νΈνμ±μ 보μ₯ν©λλ€.
ꡬν
λ€μμ μΆμ ν©ν 리 ν¨ν΄μ Python ꡬνμ λλ€:
from abc import ABC, abstractmethod
class Button(ABC):
@abstractmethod
def paint(self):
pass
class Checkbox(ABC):
@abstractmethod
def paint(self):
pass
class GUIFactory(ABC):
@abstractmethod
def create_button(self):
pass
@abstractmethod
def create_checkbox(self):
pass
class WinFactory(GUIFactory):
def create_button(self):
return WinButton()
def create_checkbox(self):
return WinCheckbox()
class MacFactory(GUIFactory):
def create_button(self):
return MacButton()
def create_checkbox(self):
return MacCheckbox()
class WinButton(Button):
def paint(self):
return "Rendering a Windows button"
class MacButton(Button):
def paint(self):
return "Rendering a Mac button"
class WinCheckbox(Checkbox):
def paint(self):
return "Rendering a Windows checkbox"
class MacCheckbox(Checkbox):
def paint(self):
return "Rendering a Mac checkbox"
# Client code
def paint_ui(factory: GUIFactory):
button = factory.create_button()
checkbox = factory.create_checkbox()
return button.paint(), checkbox.paint()
win_button, win_checkbox = paint_ui(WinFactory())
mac_button, mac_checkbox = paint_ui(MacFactory())
print(win_button) # Output: Rendering a Windows button
print(win_checkbox) # Output: Rendering a Windows checkbox
print(mac_button) # Output: Rendering a Mac button
print(mac_checkbox) # Output: Rendering a Mac checkbox
μ€λͺ :
Buttonλ°Checkbox: UI μμμ λν μΈν°νμ΄μ€λ₯Ό μ μνλ μΆμ κΈ°λ³Έ ν΄λμ€μ λλ€.WinButton,MacButton,WinCheckboxλ°MacCheckbox: Windows λ° Mac νλ«νΌμ© UI μμ μΈν°νμ΄μ€λ₯Ό ꡬννλ ꡬ체μ μΈ ν΄λμ€μ λλ€.GUIFactory: UI μμ ν¨λ°λ¦¬λ₯Ό μμ±νκΈ° μν μΈν°νμ΄μ€λ₯Ό μ μνλ μΆμ κΈ°λ³Έ ν΄λμ€μ λλ€.WinFactoryλ°MacFactory: κ°κ° Windows λ° Mac νλ«νΌμ© UI μμλ₯Ό μμ±νλ μν μ νλGUIFactoryμΈν°νμ΄μ€λ₯Ό ꡬννλ ꡬ체μ μΈ ν΄λμ€μ λλ€.paint_ui: ν©ν 리λ₯Ό μ¬μ©νμ¬ UI μμλ₯Ό μμ±νκ³ κ·Έλ¦¬λ ν΄λΌμ΄μΈνΈ ν¨μμ λλ€.
μ¬μ© μ¬λ‘
- UI νλ μμν¬: νΉμ μ΄μ 체μ λλ νλ«νΌμ λͺ¨μκ³Ό λλμ μΌμΉνλ UI μμλ₯Ό μμ±ν©λλ€.
- κ²μ κ°λ°: νΉμ κ²μ λ 벨 λλ ν λ§μ μ€νμΌμ μΌμΉνλ κ²μ κ°μ²΄λ₯Ό μμ±ν©λλ€.
- λ°μ΄ν° μ κ·Ό: νΉμ λ°μ΄ν°λ² μ΄μ€ λλ λ°μ΄ν° μμ€μ νΈνλλ λ°μ΄ν° μ κ·Ό κ°μ²΄λ₯Ό μμ±ν©λλ€.
μμ
λ€μν μ€νμΌ(μ: λͺ¨λ, λΉ ν 리μ μλ)μ κ°μ§ λ€μν μ νμ κ°κ΅¬(μ: μμ, ν μ΄λΈ)λ₯Ό μμ±νλ €λ μλ리μ€λ₯Ό κ³ λ €ν΄ λ³΄μΈμ. λ€μμ μΆμ ν©ν 리 ν¨ν΄μ μ¬μ©νμ¬ μ΄λ₯Ό ꡬννλ λ°©λ²μ λλ€:
from abc import ABC, abstractmethod
class Chair(ABC):
@abstractmethod
def create(self):
pass
class Table(ABC):
@abstractmethod
def create(self):
pass
class FurnitureFactory(ABC):
@abstractmethod
def create_chair(self):
pass
@abstractmethod
def create_table(self):
pass
class ModernFurnitureFactory(FurnitureFactory):
def create_chair(self):
return ModernChair()
def create_table(self):
return ModernTable()
class VictorianFurnitureFactory(FurnitureFactory):
def create_chair(self):
return VictorianChair()
def create_table(self):
return VictorianTable()
class ModernChair(Chair):
def create(self):
return "Creating a modern chair"
class VictorianChair(Chair):
def create(self):
return "Creating a Victorian chair"
class ModernTable(Table):
def create(self):
return "Creating a modern table"
class VictorianTable(Table):
def create(self):
return "Creating a Victorian table"
# Client code
def create_furniture(factory: FurnitureFactory):
chair = factory.create_chair()
table = factory.create_table()
return chair.create(), table.create()
modern_chair, modern_table = create_furniture(ModernFurnitureFactory())
victorian_chair, victorian_table = create_furniture(VictorianFurnitureFactory())
print(modern_chair) # Output: Creating a modern chair
print(modern_table) # Output: Creating a modern table
print(victorian_chair) # Output: Creating a Victorian chair
print(victorian_table) # Output: Creating a Victorian table
4. λΉλ ν¨ν΄
λΉλ ν¨ν΄μ 볡μ‘ν κ°μ²΄μ ꡬμ±μ ννκ³Ό λΆλ¦¬νμ¬, λμΌν κ΅¬μ± νλ‘μΈμ€κ° λ€μν ννμ μμ±ν μ μλλ‘ ν©λλ€. μ΄ ν¨ν΄μ μ¬λ¬ μ νμ κ΅¬μ± μμλ₯Ό κ°μ§ 볡μ‘ν κ°μ²΄λ₯Ό μμ±ν΄μΌ νκ³ , λ§μ μμ μμ±μλ κ΅¬μ± λ§€κ°λ³μλ₯Ό λ§λλ κ²μ νΌνκ³ μΆμ λ μ μ©ν©λλ€.
ꡬν
λ€μμ λΉλ ν¨ν΄μ Python ꡬνμ λλ€:
class Pizza:
def __init__(self):
self.dough = None
self.sauce = None
self.topping = None
def __str__(self):
return f"Pizza with dough: {self.dough}, sauce: {self.sauce}, and topping: {self.topping}"
class PizzaBuilder:
def __init__(self):
self.pizza = Pizza()
def set_dough(self, dough):
self.pizza.dough = dough
return self
def set_sauce(self, sauce):
self.pizza.sauce = sauce
return self
def set_topping(self, topping):
self.pizza.topping = topping
return self
def build(self):
return self.pizza
# Client code
pizza_builder = PizzaBuilder()
pizza = pizza_builder.set_dough("Thin crust").set_sauce("Tomato").set_topping("Pepperoni").build()
print(pizza) # Output: Pizza with dough: Thin crust, sauce: Tomato, and topping: Pepperoni
μ€λͺ :
Pizza: ꡬμΆλ 볡μ‘ν κ°μ²΄λ₯Ό λνλ΄λ ν΄λμ€μ λλ€.PizzaBuilder:Pizzaκ°μ²΄μ λ€μν κ΅¬μ± μμλ₯Ό μ€μ νλ λ©μλλ₯Ό μ 곡νλ λΉλ ν΄λμ€μ λλ€.
μ¬μ© μ¬λ‘
- λ¬Έμ μμ±: λ€μν μΉμ λ° μμ μ΅μ μ κ°μ§ 볡μ‘ν λ¬Έμ(μ: λ³΄κ³ μ, μ‘μ₯)λ₯Ό μμ±ν©λλ€.
- κ²μ κ°λ°: λ€μν μμ± λ° κ΅¬μ± μμλ₯Ό κ°μ§ 볡μ‘ν κ²μ κ°μ²΄(μ: μΊλ¦ν°, λ 벨)λ₯Ό μμ±ν©λλ€.
- λ°μ΄ν° μ²λ¦¬: λ€μν λ Έλ λ° κ΄κ³λ₯Ό κ°μ§ 볡μ‘ν λ°μ΄ν° ꡬ쑰(μ: κ·Έλν, νΈλ¦¬)λ₯Ό μμ±ν©λλ€.
μμ
λ€μν κ΅¬μ± μμ(μ: CPU, RAM, μ μ₯ μ₯μΉ)λ₯Ό κ°μ§ λ€μν μ νμ μ»΄ν¨ν°λ₯Ό ꡬμΆνλ €λ μλ리μ€λ₯Ό κ³ λ €ν΄ λ³΄μΈμ. λ€μμ λΉλ ν¨ν΄μ μ¬μ©νμ¬ μ΄λ₯Ό ꡬννλ λ°©λ²μ λλ€:
class Computer:
def __init__(self):
self.cpu = None
self.ram = None
self.storage = None
self.graphics_card = None
def __str__(self):
return f"Computer with CPU: {self.cpu}, RAM: {self.ram}, Storage: {self.storage}, Graphics Card: {self.graphics_card}"
class ComputerBuilder:
def __init__(self):
self.computer = Computer()
def set_cpu(self, cpu):
self.computer.cpu = cpu
return self
def set_ram(self, ram):
self.computer.ram = ram
return self
def set_storage(self, storage):
self.computer.storage = storage
return self
def set_graphics_card(self, graphics_card):
self.computer.graphics_card = graphics_card
return self
def build(self):
return self.computer
# Client code
computer_builder = ComputerBuilder()
computer = computer_builder.set_cpu("Intel i7").set_ram("16GB").set_storage("1TB SSD").set_graphics_card("Nvidia RTX 3080").build()
print(computer)
# Output: Computer with CPU: Intel i7, RAM: 16GB, Storage: 1TB SSD, Graphics Card: Nvidia RTX 3080
5. νλ‘ν νμ ν¨ν΄
νλ‘ν νμ ν¨ν΄μ μν μΈμ€ν΄μ€λ₯Ό μ¬μ©νμ¬ μμ±ν κ°μ²΄μ μ’ λ₯λ₯Ό μ§μ νκ³ , μ΄ μνμ 볡μ¬νμ¬ μ κ°μ²΄λ₯Ό μμ±ν©λλ€. μ΄ ν¨ν΄μ κΈ°μ‘΄ κ°μ²΄λ₯Ό 볡μ νμ¬ μ κ°μ²΄λ₯Ό μμ±ν¨μΌλ‘μ¨ μ²μλΆν° κ°μ²΄λ₯Ό μμ±ν νμλ₯Ό νΌν μ μλλ‘ ν©λλ€. μ΄λ κ°μ²΄ μμ±μ΄ λΉμ©μ΄ λ§μ΄ λ€κ±°λ 볡μ‘ν λ μ μ©ν μ μμ΅λλ€.
ꡬν
λ€μμ νλ‘ν νμ ν¨ν΄μ Python ꡬνμ λλ€:
import copy
class Prototype:
def __init__(self):
self._objects = {}
def register_object(self, name, obj):
self._objects[name] = obj
def unregister_object(self, name):
del self._objects[name]
def clone(self, name, **attrs):
obj = copy.deepcopy(self._objects.get(name))
if attrs:
obj.__dict__.update(attrs)
return obj
class Car:
def __init__(self):
self.name = ""
self.color = ""
self.options = []
def __str__(self):
return f"Car: Name={self.name}, Color={self.color}, Options={self.options}"
# Client code
prototype = Prototype()
car = Car()
car.name = "Generic Car"
car.color = "White"
car.options = ["AC", "GPS"]
prototype.register_object("generic", car)
car1 = prototype.clone("generic", name="Sports Car", color="Red", options=["AC", "GPS", "Spoiler"])
car2 = prototype.clone("generic", name="Family Car", color="Blue", options=["AC", "GPS", "Sunroof"])
print(car1)
# Output: Car: Name=Sports Car, Color=Red, Options=['AC', 'GPS', 'Spoiler']
print(car2)
# Output: Car: Name=Family Car, Color=Blue, Options=['AC', 'GPS', 'Sunroof']
μ€λͺ :
Prototype: νλ‘ν νμ μ κ΄λ¦¬νκ³ μ΄λ₯Ό 볡μ νλ λ©μλλ₯Ό μ 곡νλ ν΄λμ€μ λλ€.Car: 볡μ λ κ°μ²΄λ₯Ό λνλ΄λ ν΄λμ€μ λλ€.
μ¬μ© μ¬λ‘
- κ²μ κ°λ°: μ μ΄λ νμμ κ³Ό κ°μ΄ μλ‘ μ μ¬ν κ²μ κ°μ²΄λ₯Ό μμ±ν©λλ€.
- λ¬Έμ μ²λ¦¬: ν νλ¦Ώμ κΈ°λ°μΌλ‘ λ¬Έμλ₯Ό μμ±ν©λλ€.
- μ€μ κ΄λ¦¬: κΈ°λ³Έ μ€μ μ κΈ°λ°μΌλ‘ μ€μ κ°μ²΄λ₯Ό μμ±ν©λλ€.
μμ
λ€μν μμ±(μ: μ΄λ¦, μν , λΆμ)μ κ°μ§ λ€μν μ νμ μ§μμ μμ±νλ €λ μλ리μ€λ₯Ό κ³ λ €ν΄ λ³΄μΈμ. λ€μμ νλ‘ν νμ ν¨ν΄μ μ¬μ©νμ¬ μ΄λ₯Ό ꡬννλ λ°©λ²μ λλ€:
import copy
class Employee:
def __init__(self):
self.name = None
self.role = None
self.department = None
def __str__(self):
return f"Employee: Name={self.name}, Role={self.role}, Department={self.department}"
class Prototype:
def __init__(self):
self._objects = {}
def register_object(self, name, obj):
self._objects[name] = obj
def unregister_object(self, name):
del self._objects[name]
def clone(self, name, **attrs):
obj = copy.deepcopy(self._objects.get(name))
if attrs:
obj.__dict__.update(attrs)
return obj
# Client code
prototype = Prototype()
employee = Employee()
employee.name = "Generic Employee"
employee.role = "Developer"
employee.department = "IT"
prototype.register_object("generic", employee)
employee1 = prototype.clone("generic", name="John Doe", role="Senior Developer")
employee2 = prototype.clone("generic", name="Jane Smith", role="Project Manager", department="Management")
print(employee1)
# Output: Employee: Name=John Doe, Role=Senior Developer, Department=IT
print(employee2)
# Output: Employee: Name=Jane Smith, Role=Project Manager, Department=Management
κ²°λ‘
μμ± λμμΈ ν¨ν΄μ μ μ°νκ³ μ μ§λ³΄μ κ°λ₯ν λ°©μμΌλ‘ κ°μ²΄ μμ±μ κ΄λ¦¬νκΈ° μν κ°λ ₯ν λꡬλ₯Ό μ 곡ν©λλ€. μ΄λ¬ν ν¨ν΄μ μ΄ν΄νκ³ μ μ©ν¨μΌλ‘μ¨, λ³κ²½λλ μꡬμ¬νμ λ°λΌ νμ₯νκ³ μ μνκΈ° μ¬μ΄ λ κΉλνκ³ κ²¬κ³ ν μ½λλ₯Ό μμ±ν μ μμ΅λλ€. μ΄ κΈμμλ μ±κΈν€, ν©ν 리 λ©μλ, μΆμ ν©ν 리, λΉλ, νλ‘ν νμ μ λ€μ― κ°μ§ μ£Όμ μμ± ν¨ν΄μ μ€μ μμ λ° μ¬μ© μ¬λ‘μ ν¨κ» νꡬνμ΅λλ€. μ΄λ¬ν ν¨ν΄μ λ§μ€ν°νλ κ²μ λ₯μν Python κ°λ°μκ° λκΈ° μν νμμ μΈ λ¨κ³μ λλ€.
μ¬λ°λ₯Έ ν¨ν΄μ μ ννλ κ²μ ν΄κ²°νλ €λ νΉμ λ¬Έμ μ λ°λΌ λ¬λΌμ§λ€λ μ μ κΈ°μ΅νμΈμ. νλ‘μ νΈμ μμ± ν¨ν΄μ μ νν λ κ°μ²΄ μμ±μ 볡μ‘μ±, μ μ°μ±μ νμμ±, κ·Έλ¦¬κ³ λ―Έλ λ³ν κ°λ₯μ±μ κ³ λ €νμμμ€. κ·Έλ κ² ν¨μΌλ‘μ¨, λμμΈ ν¨ν΄μ νμ νμ©νμ¬ μΌλ°μ μΈ μννΈμ¨μ΄ λμμΈ λ¬Έμ μ λν μ°μνκ³ ν¨μ¨μ μΈ μ루μ μ λ§λ€ μ μμ΅λλ€.